package com.sc.servlet.admin.notice;

import com.alibaba.fastjson.JSONObject;
import com.sc.pojo.Field;
import com.sc.pojo.Notice;
import com.sc.services.FieldServices;
import com.sc.services.NoticeServices;
import com.sc.services.UserServices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:管理员发布公告
 * @author: hetao
 * @create: 2023-01-05 23:48
 **/
@WebServlet("/addNotice")
public class addNoticeServlet extends HttpServlet {

    private NoticeServices noticeServices = new NoticeServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        JSONObject jsonObject = JSONObject.parseObject(readLine);
        String adminname = request.getParameter("adminname");
        adminname = new String(adminname.getBytes("ISO-8859-1"), "utf-8");

        String res=null;
        if(jsonObject.getString("fieldid")==""||jsonObject.getString("fieldid")==null||jsonObject.getString("information")==null||jsonObject.getString("title")==null)
        {
            res = "not_all";
        }else {
            Notice addnotice = new Notice();
            addnotice.setAdminname(adminname);
            addnotice.setFieldid(Integer.parseInt(jsonObject.getString("fieldid")));
            addnotice.setInformation(jsonObject.getString("information"));
            addnotice.setTitle(jsonObject.getString("title"));
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String publistime = dateFormat.format(date);
            addnotice.setPublishtime(publistime);
            System.out.println(addnotice);
            try {
                noticeServices.addNotice(addnotice);
                res = "add_suc";
            }catch (Exception e){
                res="wrong_fieldid";
            }
        }






        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(res);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}